import 'package:base_flutter/base_flutter.dart';
import 'package:flutter/material.dart';
import 'package:flutter/src/widgets/framework.dart';
import 'package:ls_work/message/message_config.dart';
import 'package:ls_work/model/service_order/service_order_model.dart';
import 'package:ls_work/res/colors.dart';

import '../../net/response/service_order_ddetail_data.dart';
import '../service_order/service_order_detail_info_view_model.dart';

class OfficeRefuseOrderViewModel extends BaseViewModel {
  OfficeRefuseOrderViewModel(BuildContext context) : super(context);
  ServiceOrderDdetailData get detailData => ServiceOrderDetailInfoViewModel.detailData!;

  var model = ServiceOrderModel();
  var reason = "";

  Widget contentWidget() {
    return ListView(
      children: [
        CommonText('拒单理由').addToContainer(
            margin: EdgeInsets.symmetric(horizontal: 16, vertical: 10)),
        CommonInput(
          hintText: '请填写拒单理由',
          maxLines: 5,
          isDense: true,
          padding: EdgeInsets.zero,
          border: InputBorder.none,
          onChanged: (str){
            reason = str;
          },
        ).addToContainer(
          decoration: BoxDecoration(
            color: Colours.bg_color,
            borderRadius: 10.borderRadius,
          ),
          padding: EdgeInsets.all(10),
          margin: EdgeInsets.symmetric(
            horizontal: 16
          ),

        ),
        ElevatedButton(onPressed: (){
          refuseOrder();
        }, child: Text('提交'),
          style: ButtonStyle(
            shape: WidgetStateProperty.all(StadiumBorder()),
          ),).addToContainer(margin: EdgeInsets.symmetric(vertical: 10,horizontal: 16))
      ],
    ).addToContainer(color: Colors.white);
  }


  Future<void> refuseOrder() async {
    if(reason.isNullOrEmpty()){
      showToast('请输入拒单理由');
      return;
    }
    showLoadingDialog(msg: '提交中...');
    try{
      await model.officeRefuceOrder(reason, detailData.id.toString());
      hideDialog();
      showToast('拒单成功');
      sendMessage(SendMessageEvent(MessageConfig.REFRESH_ORDER));
      finish();
    }catch(e){
      showToast(NetErrorUtils.getNetError(e));
      hideDialog();
    }
  }
}
